package terra.model;

import terra.managers.MapManager;
import terra.managers.ThreadPoolManager;

public class MapObject {
	
	public final static byte TYPE_CUSTOM = 3;
	public final static byte TYPE_GROUND = 1;
	public final static byte TYPE_WATER = 2;
	public final static byte TYPE_AIR = 0;
	
	public int id;
	public int bgId = -1;
	public byte type = 0;
	public int x;
	public int y;
	public int hp;
	public float mass;
	
	public boolean neighbors = false;
	
	public void setType(byte t){
		if (this.type != t){
			this.type = t;
			ThreadPoolManager.getInstance().scheduleGeneral(new Runnable(){
				@Override
				public void run() {
					recalcNeighbors();
					recalc();
				}}, 1000);
		}
		
	}
	
	
	public void recalc(){
		if (type != TYPE_GROUND){
			neighbors = false;
			return;
		}
		neighbors = true;
		if (MapManager.getInstance().getTile(x-1, y) == null || MapManager.getInstance().getTile(x-1, y).type!= TYPE_GROUND)
			neighbors = false;
		else if (MapManager.getInstance().getTile(x-1, y-1) == null || MapManager.getInstance().getTile(x-1, y-1).type!= TYPE_GROUND)
			neighbors = false;	
		else if (MapManager.getInstance().getTile(x, y-1) == null || MapManager.getInstance().getTile(x, y-1).type!= TYPE_GROUND)
			neighbors = false;	
		else if (MapManager.getInstance().getTile(x+1, y-1) == null || MapManager.getInstance().getTile(x+1, y-1).type!= TYPE_GROUND)
			neighbors = false;	
		else if (MapManager.getInstance().getTile(x+1, y) == null || MapManager.getInstance().getTile(x+1, y).type!= TYPE_GROUND)
			neighbors = false;	
		else if (MapManager.getInstance().getTile(x+1, y+1) == null || MapManager.getInstance().getTile(x+1, y+1).type!= TYPE_GROUND)
			neighbors = false;	
		else if (MapManager.getInstance().getTile(x, y+1) == null || MapManager.getInstance().getTile(x, y+1).type!= TYPE_GROUND)
			neighbors = false;	
		else if (MapManager.getInstance().getTile(x-1, y+1) == null || MapManager.getInstance().getTile(x-1, y+1).type!= TYPE_GROUND)
			neighbors = false;	
	}
	
	public void recalcNeighbors(){
		MapObject o = MapManager.getInstance().getTile(x-1, y);
		if (o != null)o.recalc();
		o = MapManager.getInstance().getTile(x-1, y-1);
		if (o != null)o.recalc();
		o = MapManager.getInstance().getTile(x, y-1);
		if (o != null)o.recalc();
		o = MapManager.getInstance().getTile(x+1, y-1);
		if (o != null)o.recalc();
		o = MapManager.getInstance().getTile(x+1, y);
		if (o != null)o.recalc();
		o = MapManager.getInstance().getTile(x, y+1);
		if (o != null)o.recalc();
		o = MapManager.getInstance().getTile(x+1, y+1);
		if (o != null)o.recalc();
		o = MapManager.getInstance().getTile(x-1, y+1);
		if (o != null)o.recalc();
	}
}
